package io.pisceshub.muchat.common.core.serializer;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.core.type.WritableTypeId;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.jsontype.TypeSerializer;

import java.io.IOException;
import java.util.Date;

public class DateToLongSerializer extends JsonSerializer<Date> {

  @Override
  public void serialize(Date date, JsonGenerator jsonGenerator,
      SerializerProvider serializerProvider) throws IOException {
    jsonGenerator.writeNumber(date.getTime());
  }

  @Override
  public void serializeWithType(Date value, JsonGenerator gen, SerializerProvider serializers,
      TypeSerializer typeSer) throws IOException {
    WritableTypeId typeIdDef = typeSer.writeTypePrefix(gen,
        typeSer.typeId(value, JsonToken.VALUE_STRING));
    serialize(value, gen, serializers);
    typeSer.writeTypeSuffix(gen, typeIdDef);
  }
}
